<?php 

class NewsPresenter extends BasePresenter{
	
	
	private $model;
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup(){
		parent::startup();
		
		$this->model = NewsModel::getInstance();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender(){

	}
	
	public function boxHTML($id){
		
		$this->session = Environment::getSession("web");
		
		$this->model = NewsModel::getInstance();
		$news = $this->model->getNews(array("id_language" => $this->session->id_language));

		foreach($news as &$n){
			$n["datum"] = date("d.m.Y", strtotime($n["date"])); 
			$n["link"] = $this->link("News:", array("id"=>$id, "seo_title"=>$n["seo_title"]));
			$n["picture"] = $this->model->getPicture($n["id_file"]);
		}
		$link = $this->link("News:", array("id"=>$id));
		
		$news = array_slice($news, 0, 4);
	
		$template = new Template();
		$template->registerFilter(new LatteFilter);
		$template->news = $news;
		$template->link = $link;
		$template->setFile(APP_DIR."/modules/News/templates/front/box.phtml");
		
		return $template;
	}
	
	public function boxHTML2($id){
		
		$this->model = NewsModel::getInstance();
		
		if(array_key_exists("newsletterAdd", $_POST)){
			if($this->isValidEmail($_POST["newsEmail"])){
				
				if(!$this->model->emailExists($_POST["newsEmail"])){
					
					$this->model->emailAdd($_POST["newsEmail"]);
					
					$this->messages[] = array("message" => "Váš email byl úspěšně přidán k odběru.", "status" => "ok");
				}else{
					
					$this->messages[] = array("message" => "Tento email je již registrován k odběru.", "status" => "error");
				}
				
				
			}else{
				
				$this->messages[] = array("message" => "Tento email není validní.", "status" => "error");
			}
		}
		
		$template = new Template();
		$template->registerFilter(new LatteFilter);
		$template->link = $this->link("News:", array("id"=>$id, "seo_title"=>"rss-feed"));
		$template->translation = Translation::getInstance()->getStaticTranslation();
		$template->setFile(APP_DIR."/modules/News/templates/front/box2.phtml");
		
		return $template;
	}
	
	public function getMessages(){
		
		return $this->messages;
	}
	
	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault($id){

		$par = $this->getRequest()->getParams();
		Environment::loadConfig();
		$var = Environment::getConfig("variable");

		if(!empty($par["seo_title"]) && $par["seo_title"] != "rss-feed" && $par["seo_title"] != "odhlasit"){
			
			$address = $this->getPresenter()->link("News:", array("id" => $id, "seo_title" => $par["seo_title"]));
			
			$name = $this->model->getNewByName($par["seo_title"]);
			$name["picture"] = $this->model->getPicture($name["id_file"]);
			
			$navigace = array(
								"address" => $address,
								"name" => $name["title"]
								);
			
			$this->addToNavigation($navigace);			
			
			$this->template->new = $name;
		}elseif($par["seo_title"] == "rss-feed"){
			
			$dom = new DOMDocument('1.0', 'utf-8'); // XML version 1.0, UTF-8 encoding
			$dom->formatOutput = TRUE; // Nicely formats output with indentation and extra space
			$rss_ = $dom->createElement('rss');
			$rss_->setAttribute("version", "2.0");
			
			$rss = $dom->appendChild($rss_); // Really Simple Syndication
			$channel = $rss->appendChild($dom->createElement('channel'));
			
			// specifikace rss dokumentu
			$channel->appendChild($dom->createElement("title", "Aktuality " . $var["baseUrl"]));
			$channel->appendChild($dom->createElement("link", $var["baseUrl"]));
			$channel->appendChild($dom->createElement("description", "Aktuality serveru " . $var["baseUrl"]));
			$channel->appendChild($dom->createElement("language", "cs"));
			
			$items = $this->model->getNews(array("id_language" => $this->session->id_language));
			
			foreach ($items as $item) {
				
				$node = $channel->appendChild($dom->createElement('item'));
				$node->appendChild($dom->createElement('title', $item['title']));
				$node->appendChild($dom->createElement('link', $var["baseUrl"] . $this->getPresenter()->link("News:", array("id" => $id, "seo_title" => $item["seo_title"]))));
				$node->appendChild($dom->createElement('description', strip_tags($item['perex']))); 
				$node->appendChild($dom->createElement('pubDate', date(DATE_RFC822, strtotime($item['date']))));
			}
			
			ob_clean();
			// set header and generate
			header('Content-Type: application/xml; charset=utf-8');
			echo $dom->saveXML();
			flush();
			die();
			
		}elseif ($par["seo_title"] == "odhlasit"){
			
			$model = NewsModel::getInstance();
			
			if($model->odhlasitNewsletter($_GET["hash"])){
				
				$this->flashMessage("Váš email byl odhlášen.", "ok");
			}else{

				$this->flashMessage("Nepodařilo se odhlásit email. Zřejmě tento email není registrován k odběru.", "error");
			}
		
		}
		else{

			$this->template->news_view = $this->model->getNews(array("id_language" => $this->session->id_language));
		}
		
		$this->template->id = $id;
	}
	
	public function actionViewNew(){
		
	}
}

?>